package net.mfjassociates.financial.expense.client.managed.activity;

import net.mfjassociates.financial.expense.client.managed.request.ApplicationRequestFactory;
import net.mfjassociates.financial.expense.client.managed.request.EmployeeProxy;
import net.mfjassociates.financial.expense.client.managed.request.ReportProxy;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyDrillDownDetailsView;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyListPlace;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyPlace;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyPlace.Operation;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxy;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EmployeeDetailsActivity extends EmployeeDetailsActivity_Roo_Gwt {

	private final PlaceController placeController;

	private final ProxyDrillDownDetailsView<EmployeeProxy> view;

	private AcceptsOneWidget display;

	public EmployeeDetailsActivity(
			EntityProxyId<net.mfjassociates.financial.expense.client.managed.request.EmployeeProxy> proxyId,
			ApplicationRequestFactory requests,
			PlaceController placeController,
			ProxyDrillDownDetailsView<net.mfjassociates.financial.expense.client.managed.request.EmployeeProxy> view) {
		this.placeController = placeController;
		this.proxyId = proxyId;
		this.requests = requests;
		view.setDelegate(this);
		this.view = view;
	}

	public void deleteClicked() {
		if (!view
				.confirm("Really delete this entry? You cannot undo this change.")) {
			return;
		}
		requests.employeeRequest().remove().using(view.getValue())
				.fire(new Receiver<Void>() {

					public void onSuccess(Void ignore) {
						if (display == null) {
							return;
						}
						placeController.goTo(getBackButtonPlace());
					}
				});
	}

	public void editClicked() {
		placeController.goTo(getEditButtonPlace());
	}

	@Override
	public void drillDownClicked() {
		placeController.goTo(getDrillDownButtonPlace());
		
	}

	public Place getBackButtonPlace() {
		return new ProxyListPlace(EmployeeProxy.class);
	}

	public String getBackButtonText() {
		return "Back";
	}

	public Place getEditButtonPlace() {
		return new ProxyPlace(view.getValue().stableId(), Operation.EDIT);
	}

	public String getTitleText() {
		return "View Employee";
	}

	public boolean hasEditButton() {
		return true;
	}

	public void onCancel() {
		onStop();
	}

	public void onStop() {
		display = null;
	}

	public void start(AcceptsOneWidget displayIn, EventBus eventBus) {
		this.display = displayIn;
		Receiver<EntityProxy> callback = new Receiver<EntityProxy>() {

			public void onSuccess(EntityProxy proxy) {
				if (proxy == null) {
					placeController.goTo(getBackButtonPlace());
					return;
				}
				if (display == null) {
					return;
				}
				view.setValue((EmployeeProxy) proxy);
				display.setWidget(view);
			}
		};
		find(callback);
	}

	@Override
	public Place getDrillDownButtonPlace() {
		return new ProxyListPlace(ReportProxy.class);
	}

}
